/*
 * Main.java
 *
 * Created on June 18, 2007, 7:10 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package latihanibatis;

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.geom.Line2D;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import javax.swing.JOptionPane;
import latihanibatis.dao.MahasiswaDao;
import latihanibatis.domain.Mahasiswa;
import latihanibatis.ui.FormMahasiswa;
import latihanibatis.ui.MhsTabelModel;

/**
 *
 * @author javamaniac
 */
public class Main {
    
    private static MhsTabelModel tabelModel;
    private static Mahasiswa mhs;
    private static Reader reader;
    private static DaoManager daoManager;
    private static MahasiswaDao mhsDao;
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * Edit SplashScreen Sample from Java Tutorial, hard code to found
     * coordinate posisition.
     * See java tutorial at SplashScreen for more detail.
     */
    static void renderSplashFrame(final Graphics2D g, final int frame, final String msg) {
        // Prepare color for line
        final Color colorLine = new java.awt.Color(0, 0, 204);
        
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,100); // Set tempat utk text
        g.setPaintMode();
        g.setColor(colorLine); // set warna utk line
        // found at Java2D sample from jdk installation directory
        g.setStroke(new BasicStroke(3, 1, BasicStroke.JOIN_MITER));
        g.draw(new Line2D.Float(frame*5,177,0, 177)); // gambar line-nya.
        g.setColor(Color.BLACK); // set Warna utk text.
        g.drawString(msg, 124, 200); // Atur dimana posisi teks akan ditampilkan.
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            reader = Resources.getResourceAsReader("latihanibatis/dao/dao.xml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        daoManager = DaoManagerBuilder.buildDaoManager(reader);
        mhsDao = (MahasiswaDao)daoManager.getDao(MahasiswaDao.class);
        
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }
        
        Graphics2D g = splash.createGraphics();
        if (g == null) {
            System.out.println("g is null");
            return;
        }
        
        renderSplashFrame(g, 0, "Connecting to DataBase ...");
        splash.update();
        final String[] headerTabel = {
            "NIM","Nama", "Alamat", "Kota", "Keterangan"
        };
        tabelModel = new MhsTabelModel(headerTabel);
        try {
            renderSplashFrame(g, 30, "Connecting to DataBase ...");
            splash.update();
            final List dataMhs = mhsDao.selectAllMhs();
            Thread.sleep(180);
            
            if (dataMhs.size() <= 0) {
                JOptionPane.showMessageDialog(null, "DataBase ERROR !!!");
            } else {
                renderSplashFrame(g, 50, "Fetching Data From DataBase ...");
                splash.update();
                Thread.sleep(1800);
                for (int i=0; i<dataMhs.size(); i++) {
                    mhs = (Mahasiswa)dataMhs.get(i);
                    
                    renderSplashFrame(g, 70+i, "Preparing User Interface ...");
                    splash.update();
                    tabelModel.addMhs(mhs.getNim(), mhs.getNama(), mhs.getAlamat(),
                        mhs.getKota(), mhs.getKeterangan());
                    System.out.println(mhs.getNama());
                    Thread.sleep(180);
                }
                renderSplashFrame(g, 100, "Loading User Interface ...");
                splash.update();
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            // Null Exception
        }
        splash.close(); // Closing SplashScreen and show up the Main Frame.
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormMahasiswa(tabelModel, mhsDao).setVisible(true);
            }
        });
    }
}
